<?php


namespace app\common\model;


use think\Db;

class Commit extends Base
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'ddt_comment';
    // 主键ID
    protected $pk = 'comment_id';


    /**
     * @param array $where  // 查询的条件
     * @param string $item  // 查询的参数
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function getOne($where=[1], $item='*')
    {
        $rs = Db::table($this->table)
            ->field($item)
            ->where($where)
            ->find();

        return $rs;
    }


//    h5获取商品评论列表
    function get_product_commit($productid,$type,$limit = 10)
    {
        $where = [
            ['c.product_service_id', '=', $productid],
            ['c.is_show', '=', 1],
            ['c.type', '=', $type],
        ];

        $rs = $this
            ->alias('c')
            ->rightJoin("ddt_user u","u.user_id = c.user_id")
            ->field('c.*,u.nick_name,u.avatar')
            ->where($where)
            ->order('c.add_time desc')
            ->paginate($limit)
            ->each(function($item){
                if(!empty($item['images'])){
                    $item['images'] = explode(",",$item['images']);
                    return $item;
                }
            });
        return $rs;
    }



    //新增、编辑(后台操作)
    function edit($params, $id=0){
        if($id>0){
            // 更新条件
            $where = ['comment_id' => $id];
            $params['update_time'] = time();
            $rs = $this->allowField(true)->save($params, $where);
        }else{
            // 新增数据
            $params['add_time'] = time();
            $this->allowField(true)->save($params);
            $rs = $this->comment_id;
        }
        if ($rs === false) {
            $this->setError(-1, '操作失败');
            return false;
        }
        return $rs;
    }



}